flatten/unflatten now handle being passed null/undefined, added comments, added tests