rewriting rounding algorithm