Added comments to explain my reasoning in the code for future people.