fixed some algorithmic issues