refactoring and resolving variable error