Refactor top function.