Adds support for multiple formats (PNG default, jpeg fallback) and refactors code.