Fixed writing of fields in correct order