finished porting code, not tested yet