Part of HW3 for CS540