Added comments and made code cleaner