added some checks, changed int to uint32_t, short to uint16_t