Refactors sql, javascript, and adds some functionality