Works more or less, still a lot of optimisations possible