buf fix & refactor