Bugfixing

Fixes some bugs dedected by a bug dedection tool.