Remove redundant stacks, deep copies and a few hardcoded operations