Fixes and optimizations for bounds calculation