Fix tree sort and start to implement sync