API refactor and bug fix.