Found much better solution than mine, had to save it