Split STS into a new file

This gives better control on the source.
Also refactored some of the functions.