Major re-factoring. Lots of boilerplate stuff moved to the rest handler.