Fixing errors in buffer managment