Erlang code written by Vilpesh Mistry.