Major refactoring for API