Refactoring: extcract dataset writing structure into a separate class