Clean up and fixed buffer management infinity loop