Various fixes

Shorten variable names.
Add more error checking and tests.