Fixed search algorithm and template errors