Re-wrote to use gen_server behaviour.