Tighten up code.   Add tests for error cases.