Major problems from static code analysis fixed

I run cppcheck on LK8000 and found a lot of warnings and errors. I fixed the most important of them. Please review the changes in detail because some modifications I did may be incorrect if I incorrectly guessed what author meant writing that code.