Major refactoring. UI bugfix