More sensible way of calculating ranges, also fixes an offsset bug