mega refactoring client.js