Split factory and renderer.

Extracts sql and format functions in the process.