some effective optimizations (how could've been so idiot...)