Added some code to Xtrong's code