Removed redundant copies, and included debugging code