Fixed SQL injection security hole
Fixed code duplication when connecting to db