Avoid globals, cleanups of unused code