formatted the code and added comments