Implementation of flattening to byte array