Add copyright and doc comments; fix the failure case to be more
graceful.