Working router. Needs testing, and tidying up a little.