Added more comments for easier reading. Also tested.