modifications copied from Simo