Added Error Handler and optimized MySQL code