Some refactoring and removal of unused variables.