Identified thread safety issue on X11 and small cleanups.