added comments, and fixed some code formatting