Refactoring, minor algorithm changes