huge refactoring of code