huge refactoring/implementation