Cleanded up the log -> sql code