Added logging and error handling.