Refactored writer so that serializing is at the lowest level writing functions, higher level data structures are converted to basic data types and serialized