improving code thanks to @dereuromark