rewrite for better code pasting and better understanding