better to abstract this algorithm in an specific function