Fixes issue #23 and extracts common method