Refactorings and more cleanup