add debug env var, code cleanup