Expanding distance calculation
New bag calculation algorithm