Fixing a bug with not emptied buffer