modify code to make it more elegant