Websockets cleanup and performance optimizations

See commit messages.. a lot of stuff.