better algorithm to find interval
less nested-if, indentation
less nested-if, indentation
resetting data file