modified some code from Aj.jim feedback.