Refactored code and prepare to add a GUI interface