Made some refactoring and code separation