implementing version from lecture.
implemented version from lecture 3