Cleaned the search code and some minor changes