break the working code into subfunctions