copypaste some functions from the raoteh project