Fix typos, bugs, and simplify.

- Use common output and error code.
- Fix various typos and bugs.