Refactor; move a common routine into utils.pm