Refactored funtions into buildApi