rewritten for signed int support, some changes