refactored core data fetching and saving code