optimize naive method