Added dynamic memory allocation on encoding (whew). Code is now somewhat