refactored function betweenTwoValues for efficiency