Update by Aidil to make it more efficent.