Final code with minor edit