refactored to isolate API related functions, fix #1615