Moved the duplicated code to a function

Signed-off-by: Alexander Chuzhoy <sasha@redhat.com>