Fix undefined variable and unused imports