Added a bit more error handling.