Nicer implementation (thanks Sahib)