added comments, made code cleaner