Problem 2 implementation