Added COW handling and fixed various bugs