Fully refactored for integrating multiplayer