Refactored a bit of code and added comments to most of the code.