Refactor file writing - introduce template driven file writing