Improve memory usage; remove dead code