Refactoring and more support for primary key