Improved jpg code