modularized much of the code