relay: refactor bits of code and cleanup