Found bug in organize document function and fixed it