Moved some common code to utils.