Initial variables refactoring

Removed unused/unnecessary variables, cut off around 200 lines of
repeating code. Lowered some variables scope.