Major code refactor. Doesn't run somehow, will fix that later