put everthing into function

add while loop