add cleanup phase to make it more localstream friendly
serialize more efficiently by writing straight to the writer instead of toString and then to the writer, check for null during cleanup