First working version

Pretty messy, hasn't been tested, no examples included.