Problem 1 functional, but could use cleaning.
Problem 2 functional, but could use cleaning.
Added proper comments.