first refactorization complete with modulo but needs to be tested.
fixed order of operations issues. going to try with when nd case to replace if and elsif now