Got dynamic programming working