working version

here is a version with printf so it can be debugged
I want to upload save this one for easy debugging deleting all the
printf after this commit.