some refactoring and organizing the code