refactored out to other projects and added a few extra edge cases.