Refactor into individual functions & use C++11