First attempt at dynamic solution, seems to be working