A bit of cleanup and better error handling.