Outsourced the mysql connection.

Also removed some copy and paste stuff.