Some refactoring. Not finished!