added comments and made the code better