Handle errors to avoid crashes