Reformat the code and add more comments