Search function rewrited and moved to model. Minor improvements