Some refactoring and little improvements