Simplify the dict, and simplify the if/then logic and bounds checking.