Refactor main

Restructure and refactor main.cpp (in particular main() itself)

add space before (
(and the same thing for all the 'for's below)