added distinct, count and some fixes