Worked out bugs with global variables