Moved globals into closure to avoid conflicts