small algorithmic improvements