simplify flag parser code and bring back into main