Refactored some code and added some offline handling