First working version

Pretty messy, barely tested, no examples included.