JSON RPC

Imported old code, refactored it just a little.