working route algorithm