refactor code based on problem requirement