relay: refactor and cleanup