made a bunch of type changes to make this implementation paly more nicely with int-like types. Still quite buggy