Reorganize and rename with no change in functionality.
Add more unit tests for error conditions, fix newly discovered bugs in error handling.