refactor code. attempt to free memory used to previous search earlier
remove debug code
add minor comments