     port = Keyword.get(opts, :port, default_port())
 
     if Map.has_key?(state, port) do
-      msg =
-        with :ok <- UART.write(uart_pid(), @off),
-             :ok <- UART.drain(uart_pid()),
-             :ok <- UART.close(uart_pid()),
-             do: remove_backend(Map.get(state, port).log_path)
-
-      case msg do
+      case teardown(Map.get(state, port)) do
         :ok ->
           new_state = Map.delete(state, port)
           {:reply, msg, new_state}
 
-        _ ->
-          {:reply, msg, state}
+        {:error, error} ->
+          {:reply, {:error, error}, state}
       end
     else
-      {:reply, {:error, :ebadf}, state}
+      {:reply, {:error, :ebadf}, state} # @todo return different error atom
     end
   end
 
   def handle_info({:nerves_uart, _port, {:error, error}}, state) do
-    if @debug do
-      IO.warn("Received error message from :nerves_uart: '#{error}'")
-    end
-
+    IO.warn("Received error message from :nerves_uart: '#{error}'")
     {:noreply, state}
   end
 
-  def handle_info({:nerves_uart, port, message}, state) when !Map.has_key?(state, port) do
+  def handle_info({:nerves_uart, port, message}, state)
+    when !Map.has_key?(state, port) do
+
     {:noreply, state}
   end
 
-  def handle_info({:nerves_uart, port, message}, state) when Map.has_key?(state, port) do
-    settings = Map.get(state, port)
-    %{counter: counter, upper_limit: upper_limit} = settings
-
-    if counter == upper_limit do
-      Logger.info(message)
-
-      new_direction = -1 * direction
-      new_counter = counter + new_direction
-      new_settings = %{settings | direction: new_direction, counter: new_counter}
-      new_state = Map.put(state, port, new_settings)
-      {:noreply, new_state}
-    else
-      new_counter = counter + direction
-      new_settings = %{settings | counter: new_counter}
-      new_state = Map.put(state, port, new_settings)
-      {:noreply, new_state}
-    end
+  def handle_info({:nerves_uart, port, message}, state)
+    when Map.has_key?(state, port) do
+
+    {point_type, new_settings} = next_settings(Map.get(state, port))
+
+    case point_type do
+      :crest, :trough ->
+        Logger.info(message)
+
+      _ ->
+    end
+
+    new_state = Map.put(state, port, new_settings)
+    {:noreply, new_state}
   end
 
   def handle_info(msg, state) do

  ## Server Callbacks

  def init(:ok) do
    {:ok, _pid} = UART.start_link(name: @uart_pid)
    state = %{}
    {:ok, state}
  end


  def handle_call({:start_logging, opts}, _from, state) do
    port = Keyword.get(opts, :port, default_port())

    if Map.has_key?(state, port) do
      {:reply, {:error, :eagain}, state} # @todo return custom error atom
    else
      log_path = Path.expand(Keyword.get(opts, :log_path, @default_log_path))
      sample_rate = Keyword.get(opts, :sample_rate, @default_sample_rate)

      case set_up(port, log_path, sample_rate) do
        {:ok, settings} ->
          new_state = Map.put(state, port, settings)
          {:reply, :ok, new_state}

        {:error, error} ->
          {:reply, {:error, error}, state}
      end
    end
  end
