Cleanup of globals and deadcode