migrate latest changes from ncss, fix a couple bugs