trying to fix range problem, fixed sorting code