Refactor search logic, now useable from code