ADDED: Better error handling.