finally passes tests, had to use unsafe, though