Fixed breadth-first-search

Also altered some names and function ordering.