various bugs fixed

Converting variables to smaller types caused lots of bugs, but most of
them were fixed