Refactor manual gateway and add tests