Fix `--by-schema`
Refactor json output; add tests; fix bugs.
Add more tests; delete dead code.