Refactored ws handling