implement api and general refactoring