enhance code by Tim's comment