cleanup and fix math