Heavy refactoring, fixed a few bugs (still need to resolve global undo / redo)