This is the modified one, but still contains bugs