skeleton of the algorithm
make next into a hashmap
delete comments