Bugfixing and Code cleanup