Refactoring for perfomance

Refactoring the codes for perfomance. Doesn't look different for small
posting.