Slight optimization to search function