refactor median buffer