Bugfixes in main script