Refactor storage and optionally write dataset