Several refactorings to make things better