Refactor codes in api and console