code clean up and refinement