final version of code for S13