Moved some common code in a class for further re-use