Made some optimizations in code