working but inefficient way