Fixes for the code as promised

All different kinds of fixes were committed separately, so you could get an idea of the things to improve on by looking at individual commits :)

- Sven