Finished code for S4