Made it work with marginalia

Also, did some refactoring