Bad choice of data structure and logic...needs redesign