Cleaned up sql querys.
Bug fixes.