move code out into separate function