Extract more duplicated code in the output
Improve check for whether a schema file needs to be written