Refactoring and many cleanups, some new features