step 6 (plus some bugfixes and cleanup)