Move buffer scatter code in a function