Many changes and refactoring