refactor gateway