solved Problem 46, improved Problem 49 efficiency a tiny bit