Fixed bugs with API, and migrated popup JS to use the API as well.