Basic porting and refactoring work