Reduced to MySQL specific bits per Travell's request