merge code for #91

@guidow could you have a look?  This should address #91 and is just about ready to commit (waiting for a build to come back )