Refactor client logic and some fix