make distcheck now works

Lots of little fixed so that "make distcheck" works. Also include the fixes for make check from master.