for anyone curious this is the solution I came up with
int powmod2(int x, int e, int m) {
int y = 1;
while (e!= 0) {
if (e%2 == 0) {
e = e/2;
x = (x*x)%m;
}
else {
e = (e-1)/2;
y = (x*y)%m;
x = (x*x)%m;
}
}
return y;
}
My problem was that I was missing a step in the odd case that I was able to introduce with the y variable.
The original I needed to convert this was a recursive version written in scheme. Thanks for your help though!