Fix bugs and rework API