A little cleanup and better error handling.