Some improvements to range handling, not perfect yet