Code restructurated to make it more modular