Optimized packing and unpacking functions