fixed code based on eric's comments