Made a lot of changes

TODO
memory allocation failures