some or all of this block is duplicate... refactor the duplicate logic out into a method to share functionality?