reorganize the code; seperate functions