Fix crate collection issues

Fixes #5953, #6427, and does some code style cleanups.