Refactored image decoding code