simplified code, removed redundant variables