Implements search function, code cleanup