Refactored code out of main.cpp.