restructured main.cpp