refactored code and introduced pre-processing