some improvements, tests added.