Changed initial and final states to dynamic memory to ease freeing
memory