Improve register query (MySQL), remove some unused code