added the modified codes with fuctions