Huge code refactor. Doesnt work yet