Refactor API into factories