major code cleanup

improved many of the functions that did calculations