Improved in_range function.