refactored some stuff to writeNulls