Better error logging and handling