use malloc for count array, so the stack doesn't explode
add unit tests
code cleanup