refactoring script to include a main function and seperating sql queries
into seperate functions