Implementation of breadth first search + code cleanup.