porting code from Phoesion