improved searching

Improved the search method and added some extra error checking.