Refactor engine to use immutable map for storing last values