fixed some glaring problems and started cleaning up the code