JSON-RPC Support

Significantly cleaned up code and implemented json-rpc support.