add comments and check for edge cases for entity-manager