First working version

but horribly slow