Added some comments and reformatting for clarity.