Minor optimizations

Changed invert function to allow multiple inversions in one sketch.
Added write function in example.