Refactor to gen_server