initial porting and cleanup