first completed solution with functions