moved main program into function