Refactoring - moved costs out of the algorithm