Added comments and made code more understandable