Problem 1's solution is a little bit nicer and more general now
additional disclaimer added to problem #1