Added some JavaDoc and comments to clarify what the code does