finished reorganization into two loop functions