Move json lib code to the top of the file
Turn ajax indexing into a function