Match logic (Assignment 2); bulk of implementation