Parametrized query and error checking added.