fix a few illegal memory access issues and reorganize code for fine-grained rate estimation