More refactoring and fixing stuff