Fixed bugs with copies and optimized a bit