Refined the algorithm; added options for the Demo