refactored methods to reuse local variables and shrink code size