Reformatted some code and added more comments