Refactorings and bugfix