Code cleanup and optimization.
Merge branch 'dev' into test