huge optimization on search, see comments for details