added comments and cleanup code