Bugfixes and code clean-up, solving issue #4