Performance improvement: defer optional write() - there's another one coming later, values can be combined.
Remove unused imports/variables