Fixed error in PrepareSelect() in SQL query
Code refactoring