Fix for NullReferenceException issue

Refactored the method a bit to tidy it up while resolving this.
Merge pull request #64 from MindscapeHQ/fix-issue-63

Fix for NullReferenceException issue