Started rewriting code in order to make it more modular