Fixes distribution order bug
Adds minor code optimizations