Clean up code

Make the db variable global