Added some memory cleanup to BFS