Fixed variable scope bug