Fix key error problem and add tests #11