Removing unnecessary variables and refactoring