Refactor to OTP/gen_server in API