Simplified code, removing redundant variables.