Switch to ws, add mode option, lots of bug fixes