Implementing repacking function