Some refactoring in the editor code to make it more understandable

Still a long way to go