Big refactor in line with latest api changes