Refactor with global variables