Aligned with api.md and fixed some bugs