deleted many useless functions, optimized rearrange()