Bug fixes, added comments, and changed formatting