Variable scope bug solved.