added buffer switching logic