separate functions from main program
first working version