Finished S matrix code.