Added a lot of commentaries to clarify the code