use a more consistent and understandable absolute value comparison. change division and modulo arithmetic to use binary search.