fix invalid writes errors