Fixed type from char -> int, which caused segfault on ARM7.
Also did some cleanup of the code ..