fix bugs in math functions; style cleanup