refactoring (for better performance, use count to check whether an entity exist) (and some little refactorings)