final code for submitting to Brandon and Yev for review