Fix expire hook and add unit tests

@DeepDiver1975 @karlitschek