Added comments and improved structure of the code