Refactored WS handling