Removed unused variables and duplicate logic.