General cleanup, removed un-needed clutter

Big optimize of various parts of the code for better memory use.  Functionality should be same as before.