Fixed all errors in here and optimize it.