Cleaned up and added proper support for images (untested).