Migrate to new API, fix bugs