more comments and formatting
better handling of rounding errors