added dynamic programming