Update main.cpp

extract generic part of RH (recursion helper)