Changed write algorithm