Broke patternmatcher into more functions as things were getting too crowded.