Improved database code to be more secure