Revert "use fmod and make code simpler"

This reverts commit 66ef9bc4aa0d0bd364d20373360ad2361394a871.