Added raw proxy mode, tidied up code, fixed some race conditions.