Code cleanup

@lukechurch

This submission does code cleanup for a former submission to wrap some part of the code as functions that can be reused. This submission also makes some other changes.

One test case has been added in:
https://github.com/DynamoDS/Dynamo/pull/1992