DRY code and fixes #1