formatted the code and added more comments