major clean up and some allocation fixes (mainly insertion of deletes and delete[]s)