Not the most elegant solution but it works