Fix local/stack problem and add an optimization.