Revert "clean up code and fix divide by 0 error"

This reverts commit e45ecc499439f3747966362cc341e2fccde77d6d.