minor algorithm improvements