Formatted code slightly. Added/revised some comments