replaced some haskey() and getindex() for clarity
replaced some keys() and values() for clarity
corrected bug appearing for large orders