Refactored some of the calc code.