A bug fixed and pt4's code simplified :-).