Added error checking for search (reported by Luis Oliveira).