Used some slightly higher level code and a loop

This should make it a bit cleaner and easier to understand :)