added comments and improved