Implemented backwards search and fixed termination conditions. Hope they
are right.