Replace duplicated code by dedicated function