Cleaner imlementation (thanks @bgotink)