updated to use sql to filter results so it runs smoother