Updated code and massive refactoring