fix a bug + code clean up