reorganize algorithm