Change the codes into modularized funtions.