     Logger.debug "Received event #{inspect event}"
     if receiver == nil do
       # enqueue the event
-      new_q = :queue.in(q, event)
+      new_q = :queue.in(event, q)
       %__MODULE__{state | messages: new_q}
     else
       # send the event directly to the waiting process

    Logger.debug "State will be #{inspect state}"
    {:ok, state}
  end

  def handle_call({:hci_receive, [timeout]}, from, s = %__MODULE__{messages: q, receiver: nil}) do
    case :queue.out(q) do

        {:noreply, %__MODULE__{s | receiver: from, timer: ref}}
        # if a package arrives and a timer is running, abort the timer
        # and send a reply
      {{:value, message}, new_q} ->
        {:reply, message, %__MODULE__{s | messages: new_q}}
    end
  end
  # define a generic encoding and handling, it is not required to
  # differentiate between number of params here!
  def handle_call({func, args} = msg, from, s = %__MODULE__{port: port, calls: c})
      when is_atom(func) and is_list(args) do
    Logger.debug "Call to #{inspect func} and state #{inspect s}"
    # send a message to the port
    ref = make_ref()
