Lots of refactoring and new functionality