added comments and improved code