Avoid more precision loss. Remove debug output.
Use std::max/min to simplify code a bit.