added validator to test algorithm, fixed some bugs