big refactoring to allow building an API