Huge refactoring on code