Refactor: Use function chaining rather than matching
Refactor: Use function chaining rather than matching

And use a helper function for common functionality

matthiasbeyer/imag,matthiasbeyer/imag,mario-kr/imag,mario-kr/imag