Fix/misc

code cleanup and lots of additional optional checking
fixes #120