rebuilding algorithm yet again
working on new algo