More refactoring, cleaning, and general fixes