Solved problem 3. Could probably use a bit of refactoring, but good enough