Bugfixes, typo corrections and some code cleanup