Moved SM code out of server

now in its own file for clarity.