Remove unused variables and optimize code