added comments, readibility, fixed bug:used unset variable