added aman's code....it runs much faster.