Refactor core into API