refatoring and adding comments