implemented cost calculation