optimize seen_materials checking performance by pre save it to an array
eager load materials with files
futher refine code