Edited code for readability, and fixed a but in keyExists.