Refactor error handlings