added slightly modular structure to the code