Add @seealso in comments and extract some common code into a function.