Added comments explaining logic