refactored array and sql