Minor refactoring to writeSamples.