Fixed new object vs new datastream confusion and some refactoring of the code for more modular functions