Some code reshuffeling and removing duplication