Added createER logic and major refactoring