Major refactoring; doesn't work yet
FIxes, refactoring