fixed, hopefully, a bug causing crashes

specifically, stack overflow errors being caused by having to flatten
structures that were nested too deep. added a try/catch and option wrap.