Erlang refactoring and minor changes