Refactored as suggested by @mislav, much cleaner :+1: