First take on the problem.
Working version - passes the tests
Added comments