Solution for 04 (plus some refactoring)