Cleaned-up mess after new features and refactoring, added new tests