Optimization and minor fixes
Optimization and minor fixes #2