Refactor algorithm slightly thereby significantly improving performance.

Signed-off-by: Peter Atashian <retep998@gmail.com>