I also pulled this logic out into it's own function to be a bit more explicit and testable.