JSON-RPC Support added

Significantly cleaned up code and implemented json-rpc support.