Adding sub-functions and re-organizing main functions