Moved capture logic into separate function