Refactoring. Minor fixes