added deferred close with error check and compression