Final fix for mid() memory issue