Fixes issue #510 and #156, as well as issues addressed in PR #373.
Incorperates more changes from #373 in order to fix issue #296

Fixed minor formatting issues and removed <139 lines of whitespace