relay: more refactorings and cleanups