algorithm improved & minor fixes