split legacy code from main program, minor code changes