error handling
fix problem with missing key, slight refactor