fixed search and cleaned up code