Commented most of the code to help make it easier to understand.