Revised code completely.