Refactor, cleanup and improvements
Bug fix: value instead of key was used as default