Updated code with Eskandar's changes