extract shared code to global vars