Remove unused variables, avoid copies where possible, check empty() instead of size() == 0
Most of these were detected by cppcheck