Reduce flickering slightly by using weighted average

The code is ugly because I don't know how to use generate statements...