Fixes, cleanup, and performance improvements