Many bug fixes and refactoring. Added test mode "ONE" that outputs one
model per test.