arrange and remove redundant code