Updated code based on comments