Added more logic to Fix #644