First steps towards allowing a server

Significantly modularized the code. A lot more function calls