Reshaped respond function and introduced proper error handling