complete rewrite, bug fix & performance tuning