this algorithm should be commented... 
this might not be obvious how it works for someone else.