Refactor codes in the api