Implemented some code fixes

Some fixes, some clean ups, some speed ups.