Code improvements for generate summary function