More refactoring etc. pretty sure i broke stuff