beautifying a little.
moved the common code to util.