Refactoring

- Add JSON dependency.
- Coding style consistency (it's now mostly consistent...).
- Namespace consistency.
- Refactor code into classes as much as possible.
- Better thread handling instead of relying on sleep.