removing most of the code from main to elsewhere