code cleanups, error handling additions, functionality additions.

Fixes #2, #4