Revamp the gateway logic to be faster