Cleanup and refactoring

* Simplified a lot of MySQL stuff and removed dependencies on mysqli module
* Refactored a lot of code for simplicity and consistency
* Much improved the output