refactoring on sample code