Major cleanup, namespace variables, optimization