Fixed potential bug in error handling