Made the script more modular
Fixed the database connection