Refactor apiserver