Fix initializations and distance computation