code refactor trying to fix bug