Remove unnecessary lines and correct variable scope