Refactoring entity tests and fixing some minor bugs