Initial working code

Not systematically tested yet.