refactored range correction code