Random Zero Results

For some ungodly reason

The reason is that the division was being done with integer arithmetic, as others have said.

What i don't understand is why its all perfect, but then just randomly goes to zero

It wasn't random, it was giving you a zero every time the result of division by 1023 gave a result that was less than 1. This is what you'd expect with integer division. Floating-point division will give you a fractional result, which makes the overall calculation give you the result you want. It's much slower, of course.