EntityManager optimizations and fixes