Factor global variable stuff into own method