Move most code from rbs into main.