fix a bug and code clean up