Fix bugs, simplify SQL, and improve the code layout in line with the guidelines
Merge pull request #354 from timschofield/develop

Fix bugs, simplify SQL, and improve the code layout in line with the gui...