Better logging and error handling