version 2 of the original code file 

with  some bug fixes