Refactored to make error handling easier