Code cleanup - part 1

I push first part now, because I don't want to cause many merge conflicts in possible future pull request pushed by others.

See also detailed descriptions in commit messages.