Bug 831748 - refactor duplicated code. r=froydnj.