Some restructuring, error catching, API cleanup, and a test script.