Merging #320 to master

needs more review and testing