first draft - transposed from erlang