removing unused variables and simplifying the code