Fix for the invalid range argument issue. Credit goes to Karl Cronburg.