Json support. Some refactoring.