improved cost calculation; fixed cache updating