refactoring table gateway