completed, but still buggy. added sum distinct functions.