move search to a function, still ugly