added buffer state save and restore