Refactor unknown purpose code in separate func