fixed overflow counting, minor code cleanup