tiny bit of optimization to the naive method ;)