More idiomatic Erlang

Use #state{...} = State to destructure records, instead of doing
State#state.$foo all over the place.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>