Fixes #21
Improved error checking and documentation