fixing bug with using same variable twice