fixed minor bugs in the main algorithm