added comments and made code neater