Huge refactor, supports multiple protocols