Reorganize and delete unnecessary code