OP-1586 Re-factored to get rid of duplicated code.