Port more functions and reorganize code to match the origin