fixes #48 and adds a few useful functions
Further refinements and bugfixes to use updated functions and structures