Added comments and documentation and refactored some variable names.