merge with previous attempt for porting