Refactor ex2-3.cpp