Added test and fixed the closing condition