Refactored problems 36, 37, 39 & 40