reorder and edit some code