Fixups, optimize, add progressbar

Calculate cost function on sum, add progress bar, make possible to save
and load cost arrays from files.