moved several functions, reduced math boilerplate