fixed bugs and removed unecessary copies of data