Refactor {to,from}_rgmlgraph to reduce complexity