Database Fixes, Code Cleanups, Removal of Nested loops and if statements that are not necessary

See each commit for changes.