First fixes and refactoring