Refactored some methods to take advantage of pattern matching.