Little refactoring and fixes