Remove unused variables and clean up code