Big refactor to simplify the internal API