tweaks and better error handling in api.js