rewrite as a GenServer