added comments and fixed error flags