Wrote it from scratch again and it almost works