some refactoring/cleanup to remove duplication
added more patient tests