main implementation of pack_data