Started reworking the code, currently slightly broken