Bugfixes, code reformatting