Made some refactoring and improvements