Remove duplicate code
Add optimization of vars