Finished code for S2