A first try to implement cost function design;

Uses inner loop and is too heavy, so the goal is to implement a more efficient
version of this idea.