Tidy up the code, put some guards on the invocation. Fixes #3, I suppose.