Initially based on pull request #40 by hochwasser <oo.o+github@windbuechse.samba-tng.org>, with refactoring.