Templates now work. Fixed a bunch of memory leaks and invalid read/writes as well.