more refactoring, JSON generation mostly works now