refactor code for packageBuffer