Added comments explaining the program logic