optimise search using a queue