First working (but not thoroughly tested) version! Use at your own risk.