Assorted fixes, improvements, and refactoring