Add key callback and use safer modification functions