some code improvement and cleaning up