General refactoring and API changes