Improved search function. Fixes #256