use C++1y closures to simplify the code