Fix compiler warnings and remove dead code

Fix several -Werror=unused-variable and -Werror=unused-but-set-variable
and removed dead code found with this warnings