working issue #56

a bit of testing and debugging, but I think it's mostly ok.
needs more testing.