Refactor api out of main (stable)