Fix range calculation bug