Theoretically working version.

Needs testing!