Abstract out iterative search function. Fixes #2