Based off of mathnerd314's patch to fix issue #327