Fix leaking globals and add test case