Commented on most of the code, to make things more clear