fixed search. Some code improvements and bugfixes