Merged with some code from technomancy...