Imported code from Retistruct package