changed range calculating code to make more efficient. must continue optimization or code will not complete soon