refactor out reusable runtime logic