Added better error handling for API read failures, and refactored state
code to reduce duplication of logic