Tidying up the code and seperate them to several function