this seems to work, but i don't understand some things