Refactor sample codes for generating tables function.