Refactoring mostly working