Refactor into api module