Optimized #1959 code a bit