some mostly untested code from back in the day